package com.jiyun.controller;

import com.github.pagehelper.PageInfo;
import com.jiyun.pojo.Activity;
import com.jiyun.service.ActivityService;
import com.jiyun.vo.ResultCommon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @RequestMapping("findAll")
    public ResultCommon findAll(@RequestBody Activity activity,Integer pageNo,Integer pageSize){
        PageInfo<Activity> pageInfo = activityService.findAll(activity,pageNo,pageSize);
        return ResultCommon.suceessData(pageInfo);
    }
    @DeleteMapping("delete")
    public ResultCommon delete(Integer id){
        activityService.delete(id);
        return ResultCommon.suceess();
    }
    @RequestMapping("findsave")
    public ResultCommon findsave(@RequestBody Activity activity){
        try {
            if (activity.getId()==null){
                activityService.add(activity);
            }else{
                activityService.update(activity);
            }
            return ResultCommon.suceessData("yes");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultCommon.error("no");
    }
    @RequestMapping("findById")
    public ResultCommon findById(Integer id){
        Activity activity = activityService.findById(id);
        return ResultCommon.suceessData(activity);
    }
}
